package dao

import (
	"context"
	"time"

	"github.com/go-redis/redis/v8" // 注意导入的是新版本
)

var (
	Rdb *redis.Client
)

// 初始化连接
func InitRdbClient() (err error) {
	Rdb = redis.NewClient(&redis.Options{
		Addr:     "47.97.229.166:6379",
		Password: "maizs2021Redis", // no password set
		DB:       1,                // use default DB
		PoolSize: 100,              // 连接池大小
	})

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	_, err = Rdb.Ping(ctx).Result()
	return err
}

func RdbSet(key string, value interface{}, expire time.Duration) error {
	ctx := context.Background()
	return Rdb.Set(ctx, key, value, expire).Err()
}

func RdbGet(key string) (string, error) {
	ctx := context.Background()
	return Rdb.Get(ctx, key).Result()
}

func RdbLPush(key string, data string) *redis.IntCmd {
	ctx := context.Background()
	return Rdb.LPush(ctx, key, data)
}

func RdbBRPop(keys ...string) ([]string, error) {
	ctx := context.Background()
	return Rdb.BRPop(ctx, 0, keys...).Result()
}
